<?php
/**
 * Digg pagination style
 *
 * @preview  « Previous  1 2 … 5 6 7 8 9 10 11 12 13 14 … 25 26  Next »
 */
$page = Zend_Controller_Front::getInstance()->getRequest()->getParam('page');

$this->total_items = (int)max(0, $this->totalItemCount);
$this->items_per_page = (int)max(1, $this->itemCountPerPage);
$this->total_pages = (int)ceil($this->total_items / $this->items_per_page);
$this->current_page = (int)min(max(1, $page), max(1, $this->total_pages));
$this->current_first_item = (int)min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items);
$this->current_last_item = (int)min($this->current_first_item + $this->items_per_page - 1, $this->total_items);

// If there is no first/last/previous/next page, relative to the
// current page, value is set to FALSE. Valid page number otherwise.
$this->first_page = ($this->current_page == 1) ? FALSE : 1;
$this->last_page = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages;
$this->previous_page = ($this->current_page > 1) ? $this->current_page - 1 : FALSE;
$this->next_page = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE;

/**
 * Grabli for q_id
 */

$valid_params = array('order', 'order_field');
$requestUri = \Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();
$query = parse_url($requestUri, PHP_URL_QUERY);
$path = parse_url($requestUri, PHP_URL_PATH);

$params = urldecode($query);

parse_str($params, $params);

$navigation_params = array();

foreach ($params as $key => $item) {
    if (in_array($key, $valid_params)) {
        $navigation_params[$key] = $item;
    }
}
;

$controllerName = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
$path = '/';
switch ($controllerName) {
    case 'report':
        $path = '/report/page';
        break;
}
$result_url = http_build_query($navigation_params);
$url = $path . '/';



?>


<div class="bar corner-all paginator">
    <ul class="paginator-content paginator-background-spr line-background">
        <li>
            <?php if ($this->previous_page): ?>
            <a class="paginator-background-spr previous-page" href="<?php echo $url . $this->previous_page; ?>"></a>
            <?php else: ?>

            <a class="paginator-background-spr previous-page" href="../" onclick="return false;"></a>
            <?php endif ?>
        </li>

        <?php if ($this->total_pages < 11): /* « Previous  1 2 3 4 5 6 7 8 9 10 11 12  Next » */ ?>

        <?php for ($i = 1; $i <= $this->total_pages; $i++): ?>
            <?php if ($i == $this->current_page): ?>
                <li class="active"><a class="paginator-background-spr s-active-bg" href="../"
                                      onclick="return false;"><?php echo $i ?></a> <span></span></li>
                <?php else: ?>
                <li><a href="<?php echo $url . $i; ?>"><?php echo $i ?></a><span></span></li>
                <?php endif ?>
            <?php endfor ?>

        <?php elseif ($this->current_page < 7): /* « Previous  1 2 3 4 5 6 7 8 9 10 … 25 26  Next » */ ?>

        <?php for ($i = 1; $i <= 8; $i++): ?>
            <?php if ($i == $this->current_page): ?>
                <li class="active"><a class="paginator-background-spr s-active-bg" href="../"
                                      onclick="return false;"><?php echo $i ?></a> <span></span></li>
                <?php else: ?>
                <li><a href="<?php echo $url . $i; ?>"><?php echo $i ?></a><span></span></li>
                <?php endif ?>
            <?php endfor ?>

        <li class="paginator-background-spr dotted"></li>
        <li>
            <a href="<?php echo $url . ($this->total_pages - 1); ?>"><?php echo $this->total_pages - 1 ?></a><span></span>
        </li>
        <li><a href="<?php echo $url . $this->total_pages; ?>"><?php echo $this->total_pages ?></a><span></span></li>

        <?php  elseif ($this->current_page > $this->total_pages - 6): /* « Previous  1 2 … 17 18 19 20 21 22 23 24 25 26  Next » */ ?>

        <li><a href="<?=$url . "1"; ?>">1</a><span></span></li>
        <li><a href="<?=$url . "2"; ?>">2</a><span></span></li>
        <li class="paginator-background-spr dotted"></li>

        <?php for ($i = $this->total_pages - 7; $i <= $this->total_pages; $i++): ?>
            <?php if ($i == $this->current_page): ?>
                <li class="active"><a class="paginator-background-spr s-active-bg" href="../"
                                      onclick="return false;"><?php echo $i ?></a> <span></span></li>
                <?php else: ?>
                <li><a href="<?php echo $url . $i; ?>"><?php echo $i ?></a><span></span></li>
                <?php endif ?>
            <?php endfor ?>

        <?php  else: /* « Previous  1 2 … 5 6 7 8 9 10 11 12 13 14 … 25 26  Next » */ ?>

        <li><a href="<?=$url . '1'; ?>">1</a><span></span></li>
        <li><a href="<?=$url . '2'; ?>">2</a><span></span></li>
        <li class="paginator-background-spr dotted"></li>

        <?php for ($i = $this->current_page - 4; $i <= $this->current_page + 4; $i++): ?>
            <?php if ($i == $this->current_page): ?>
                <li class="active"><a class="paginator-background-spr s-active-bg" href="../"
                                      onclick="return false;"><?php echo $i ?></a> <span></span></li>
                <?php else: ?>
                <li><a href="<?php echo $url . $i; ?>"><?php echo $i ?></a><span></span></li>
                <?php endif ?>
            <?php endfor ?>

        <li class="paginator-background-spr dotted"></li>
        <li>
            <a href="<?php echo $url . ($this->total_pages - 1); ?>"><?php echo ($this->total_pages - 1); ?></a><span></span>
        </li>
        <li><a href="<?php echo $url . $this->total_pages; ?>"><?php echo $this->total_pages ?></a><span></span></li>

        <?php endif ?>

        <li class="last">
            <?php if ($this->next_page): ?>
            <a class="paginator-background-spr next-page" href="<?php echo $url . $this->next_page; ?>"></a>
            <?php else: ?>
            <a class="paginator-background-spr next-page" onclick="return false;"
               href="<?php echo $url . $this->next_page; ?>"></a>
            <?php endif ?>
        </li>

    </ul>
</div>